package com.ltg.urban.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ltg.framework.properties.LoginProperties;
import com.ltg.urban.filter.LoginFilter;
import com.ltg.urban.resolver.UserArgumentResolver;
import jakarta.servlet.Filter;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;


/**
 * <p> ClassName: WebMvcConfig </p>
 * <p> Package: com.ltg.urban.config </p>
 * <p> Description: </p>
 * <p></p>
 *
 * @Author: LTG
 * @Create: 2023/2/11 - 20:18
 * @Version: v1.0
 */

@Configuration
@RequiredArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {

    private final LoginProperties loginProperties;
    private final RedisTemplate<String, Object> redisTemplate;

    private final ObjectMapper objectMapper;

    private final UserArgumentResolver userArgumentResolver;

    /**
     * 添加登录过滤器
     */
    @Bean
    public FilterRegistrationBean<Filter> loginFilterRegistration() {
        // 注册LoginFilter
        FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new LoginFilter(redisTemplate, loginProperties, objectMapper));
        // 设置名称
        registrationBean.setName("loginFilter");
        // 设置拦截路径
//        registrationBean.addUrlPatterns(loginProperties.getFilterIncludeUrl().toArray(List.));
        // 指定顺序，数字越小越靠前
        registrationBean.setOrder(1);
        return registrationBean;
    }


    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(userArgumentResolver);
    }
}
